package com.lanqiao.companymanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanqiao.companymanage.entity.User;
import com.lanqiao.companymanage.mapper.StaffMessMapper;
import com.lanqiao.companymanage.mapper.UserMapper;
import com.lanqiao.companymanage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    StaffMessMapper staffMessDao;

    /**
     * 分页查询
     */
    @Override
    public IPage<User> listPage(Page<User> userPage) {
        userPage = baseMapper.listPage(userPage);
        return userPage;
    }


    /**
     * 根据员工编号(userId)查询
     * @param userId
     * @return
     */
    @Override
    public User selectByUserId(Integer userId) {
        return baseMapper.selectUserByUserId(userId);
    }

    /**
     * 员工信息添加
     * 名称、人数、工资系数
     * @param user
     * @return
     */
    @Override
    @Transactional
    public int addUser(User user) {
        // 插入员工
        baseMapper.insert(user);
        // 插入关联表
        int i = baseMapper.insertUser(user.getUserId(), user.getDep().getDepId());
        return i;
    }


    /**
     * 根据员工编号userId删除
     */
    @Override
    @Transactional
    public int deletedByUserId(Integer userId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserId,userId);
        int delete = baseMapper.delete(wrapper);
        baseMapper.deleteUserTea(userId);
        return delete;
    }

    /**
     * 根据userId修改
     */
    @Override
    @Transactional
    public int updateUser(User user) {
        int update = baseMapper.updateById(user);
        baseMapper.deleteUserTea(user.getUserId());
        baseMapper.insertUser(user.getUserId(), user.getDep().getDepId());
        return update;
    }


    /**
     * 查询全部员工信息
     * @return
     */
    @Override
    public List<User> getAllUser() {
        return baseMapper.getAllUser();
    }
}
